﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;

namespace CurvaLauncher.Services
{
    public class AssemblyResourceKey : ResourceKey, IEquatable<AssemblyResourceKey>
    {
        public AssemblyResourceKey(Assembly assembly, object key)
        {
            Assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
            Key = key;
        }

        public override Assembly Assembly { get; }
        public object Key { get; }

        public override bool Equals(object obj)
        {
            return Equals(obj as AssemblyResourceKey);
        }

        public bool Equals(AssemblyResourceKey other)
        {
            if (other == null)
                return false;

            return EqualityComparer<Assembly>.Default.Equals(Assembly, other.Assembly) &&
                   EqualityComparer<object>.Default.Equals(Key, other.Key);
        }

        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + (Assembly != null ? Assembly.GetHashCode() : 0);
            hash = hash * 23 + (Key != null ? Key.GetHashCode() : 0);
            return hash;
        }

        public static bool operator ==(AssemblyResourceKey left, AssemblyResourceKey right)
        {
            if (ReferenceEquals(left, right))
                return true;

            if (left is null || right is null)
                return false;

            return left.Equals(right);
        }

        public static bool operator !=(AssemblyResourceKey left, AssemblyResourceKey right)
        {
            return !(left == right);
        }
    }
}